package com.stream.service.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.stream.model.entity.SysMenuEntity;
import com.stream.repository.SysMenuRepository;
import com.stream.service.SysMenuService;
import com.stream.service.SysRoleMenuService;
import com.stream.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import static com.stream.utils.Constant.MenuType.CATALOG;

/**
 * Created by yebaokang on 2017/5/23.
 */

@Service("sysMenuService")
public class SysMenuServiceImpl implements SysMenuService {

    //加这个注解让IDE 不报: Could not autowire
    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Autowired
    private SysMenuRepository sysMenuRepository;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @Override
    public List<SysMenuEntity> queryListParentId(Long parentId, List<Long> menuIdList) {
        List<SysMenuEntity> menuList = sysMenuRepository.queryListParentId(parentId);
        if (menuIdList == null) {
            return menuList;
        }

        List<SysMenuEntity> userMenuList = Lists.newArrayList();
        for (SysMenuEntity menu : menuList) {
            if (menuIdList.contains(menu.getMenuId())) {
                userMenuList.add(menu);
            }
        }

        return  userMenuList;
    }

    @Override
    public List<SysMenuEntity> queryNotButtonList() {
        return sysMenuRepository.queryNotButtonList();
    }

    @Override
    public List<SysMenuEntity> getUserMenuList(Long userId) {
        if (userId == 1) {
            return getAllMenuList(null);
        }
        //用户菜单列表
        List<Long> menuIdList = sysUserService.queryAllMenuId(userId);
        return getAllMenuList(menuIdList);
    }

    @Override
    public Set<String> getUserPermissions(Long userId) {
        List<String> permsList;

        //系统管理员，拥有最高权限
        if (userId == 1) {
            List<SysMenuEntity> menuList = queryList(new HashMap<String,Object>());
            permsList = new ArrayList<>(menuList.size());
            for (SysMenuEntity menu : menuList) {
                permsList.add(menu.getPerms());
            }
        }else{
            permsList = sysUserService.queryAllPerms(userId);
        }

        //用户权限列表
        HashSet<String> permsSet = Sets.newHashSet();
        for (String perms : permsList) {
            if (StringUtils.isBlank(perms)) {
                continue;
            }
            permsSet.addAll(Arrays.asList(perms.trim().split(",")));
        }

        return permsSet;
    }

    @Override
    public SysMenuEntity queryObject(Long menuId) {
        return sysMenuRepository.queryObject(menuId);
    }

    @Override
    public List<SysMenuEntity> queryList(Map<String, Object> map) {
        return sysMenuRepository.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return sysMenuRepository.queryTotal(map);
    }

    @Override
    public void save(SysMenuEntity menu) {
        sysMenuRepository.save(menu);
    }

    @Override
    public void update(SysMenuEntity menu) {
        sysMenuRepository.update(menu);
    }

    @Override
    public void deleteBatch(Long[] menuIds) {
        sysMenuRepository.deleteBatch(menuIds);
    }

    @Override
    public List<SysMenuEntity> queryUserList(Long userId) {
        return sysMenuRepository.queryUserList(userId);
    }

    private List<SysMenuEntity> getAllMenuList(List<Long> menuIdlist) {
        //查询跟菜单列表
        List<SysMenuEntity> menuList = queryListParentId(0L, menuIdlist);

        //递归获取子菜单
        getMenuTreeList(menuList, menuIdlist);

        return menuList;
    }

    private List<SysMenuEntity> getMenuTreeList(List<SysMenuEntity> menuList, List<Long> menuIdList) {
        ArrayList<SysMenuEntity> subMenuList = new ArrayList<>();
        for (SysMenuEntity menu : menuList) {
            if (menu.getType() == CATALOG.getValue()) {
                menu.setList(getMenuTreeList(queryListParentId(menu.getMenuId(), menuIdList), menuIdList));
            }
            subMenuList.add(menu);
        }

        return subMenuList;
    }
}
